package com.hnu.highlynewapp.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hnu.highlynewapp.entity.TechnologyNeedEntity;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.utils.ValidationUtils;
import com.hnu.highlynewapp.vo.DurationFieldVo;
import com.hnu.highlynewapp.vo.PublishServiceProductVo;
import com.hnu.highlynewapp.vo.PublishTechAchVo;
import com.hnu.highlynewapp.vo.TechnologyAchievementVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;

import com.hnu.highlynewapp.entity.TechnologyAchievementEntity;
import com.hnu.highlynewapp.service.TechnologyAchievementService;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 技术成果
 *
 * @author hnu_worker
 * @email 825963704@qq.com
 * @date 2021-01-03 10:35:56
 */
@Api(tags = "技术api管理")
@RestController
@RequestMapping("/technologyachievement")
public class TechnologyAchievementController {
    @Autowired
    private TechnologyAchievementService technologyAchievementService;

    /**
     * 发布技术成果
     */
    @ApiOperation("发布技术成果")
    @PostMapping("/publish")
    public R publish(@Validated @RequestBody PublishTechAchVo vo, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = ValidationUtils.getErrors(result);
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data",map);
        } else {
            technologyAchievementService.publish(vo);
            return R.ok();
        }
    }

    /**
     * 根据发布者id和类型获取全部的技术成果
     * @param technologyAchievementVo
     * @return
     */
    @ApiOperation("根据发布者id和类型查询全部的技术成果")
    @PostMapping("/listByPublisherIdType")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = TechnologyAchievementEntity.class)
    })
    public R listByPublisherID(@RequestBody TechnologyAchievementVo technologyAchievementVo) {
        List<TechnologyAchievementEntity> technologyAchievementEntities = technologyAchievementService.listByPublisherID(technologyAchievementVo.getPublisherId(),technologyAchievementVo.getPublisherType());
        return R.ok().put("data",technologyAchievementEntities);
    }

    /**
     * 根据技术成果id显示技术成果详细信息
     * @param technologyAchievementId
     * @return
     */
    @ApiOperation("根据技术成果id查询技术成果详细信息")
    @GetMapping("/details/{technologyAchievementId}")
    @ApiResponses({
            @ApiResponse(code = 10003, message = "查询的信息不存在"),
            @ApiResponse(code = 0,message = "success",response = TechnologyAchievementEntity.class)
    })
    public R details(@PathVariable("technologyAchievementId") @ApiParam(value = "技术成果id",required = true) Long technologyAchievementId){
        TechnologyAchievementEntity technologyAchievementEntity = technologyAchievementService.getById(technologyAchievementId);
        if (technologyAchievementEntity == null) {
            return R.error(BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getCode(),BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getMessage());
        }

        return R.ok().put("technologyNeed", technologyAchievementEntity);
    }

    /**
     * 列表
     */
    @ApiIgnore
    @RequestMapping("/list")
    //@RequiresPermissions("highlynewapp:technologyachievement:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = technologyAchievementService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiIgnore
    @RequestMapping("/info/{technologyAchievementId}")
    //@RequiresPermissions("highlynewapp:technologyachievement:info")
    public R info(@PathVariable("technologyAchievementId") Long technologyAchievementId){
		TechnologyAchievementEntity technologyAchievement = technologyAchievementService.getById(technologyAchievementId);

        return R.ok().put("technologyAchievement", technologyAchievement);
    }

    /**
     * 保存
     */
    @ApiIgnore
    @RequestMapping("/save")
    //@RequiresPermissions("highlynewapp:technologyachievement:save")
    public R save(@RequestBody TechnologyAchievementEntity technologyAchievement){
		technologyAchievementService.save(technologyAchievement);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @RequestMapping("/update")
    //@RequiresPermissions("highlynewapp:technologyachievement:update")
    public R update(@RequestBody TechnologyAchievementEntity technologyAchievement){
		technologyAchievementService.updateById(technologyAchievement);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @RequestMapping("/delete")
    //@RequiresPermissions("highlynewapp:technologyachievement:delete")
    public R delete(@RequestBody Long[] technologyAchievementIds){
		technologyAchievementService.removeByIds(Arrays.asList(technologyAchievementIds));

        return R.ok();
    }

    /**
     * 根据时间和行业领域检索
     * @param vo
     * @return
     */
    @PostMapping("/listByCondition")
    @ApiOperation("根据时间和行业领域查询技术成果")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = TechnologyAchievementEntity.class)
    })
    public R listByCondition(@RequestBody DurationFieldVo vo) {
        return R.ok().put("data",technologyAchievementService.listByCondition(vo.getDuration(), vo.getField()));
    }
}
